#include "file.h"

struct String read_line(struct Memory *mem, FILE *file) {
  struct String str = init_string();
  while (true) {
    int i = fgetc(file);
    if (i == EOF) {
      break;
    } else {
      struct MemResult res = new_memory(mem, sizeof(char) * 2);
      char c = (char)i;
      char *s = (char*)res.data;
      s[0] = c;
      s[1] = '\0';
      if (c == '\r') {
	continue;
      } else if (c == '\n') {
	break;
      }
      str = str_append(mem, str, s);
      free_memory(mem, res.id);
    }
  }
  return str;
}
